package com.example.quartz.controller;

import com.example.quartz.HelloJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description : 手动执行：testController
 * @Author : Bruce Lee
 * @CreateTime : 2024/2/6
 */


@RestController
@RequestMapping("/quartz")
public class TestController {

    @Autowired
    private Scheduler scheduler;

    @GetMapping("/test")

    public String test() throws SchedulerException {

        // 添加一个任务实例
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .withIdentity("job1", "group1")
                .usingJobData("name", "张三")
                .build();

        // 添加触发器实例: 每秒执行一次，任务永不停止
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())
                .build();

        // 开始进行任务调度
        scheduler.scheduleJob(job, trigger);

        return "success";
    }

}
